package com.example.demo.handler;

import com.example.demo.processor.Processor;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

public class WebsocketHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {

    private Processor processor = new Processor();
    @Override
    protected void messageReceived(ChannelHandlerContext channelHandlerContext, TextWebSocketFrame textWebSocketFrame) throws Exception {
        System.out.println(textWebSocketFrame.text());
        processor.handler(channelHandlerContext.channel(),textWebSocketFrame.text());
        channelHandlerContext.writeAndFlush(new TextWebSocketFrame("你也好！"));
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        super.handlerRemoved(ctx);
        processor.loginOut(ctx.channel());
    }
}
